package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张杰
 * Date: 2022-04-07
 * Time: 21:10
 */
class Counter {
    public int count;
    synchronized public void increase() {
        count++;
    }
}
public class Demo2 {
    private static Counter counter = new Counter();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() ->{
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });
        Thread t2 = new Thread(() ->{
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });
        t1.start();
        t2.start();

        //必须要在t1和t2都执行完成之后再打印count
        t1.join();
        t2.join();

        //在main中打印一下两个线程自增完成后得到的count结果
        System.out.println(counter.count);
    }
}
